from playhouse.pool import PooledMySQLDatabase
from playhouse.shortcuts import ReconnectMixin
import nacos
import json


class ReconnectMysqlDatabase(ReconnectMixin, PooledMySQLDatabase):
    pass


NACOS = {
    "Host": "192.168.178.120",
    "Port": 8848,
    "NameSpace": "53736c83-095d-4448-8525-3a8109f03aed",
    "User": "nacos",
    "Password": "nacos",
    "DataId": "user_srv.debug",
    "Group": "debug",
}

client = nacos.NacosClient(f'{NACOS["Host"]}:{NACOS["Port"]}', namespace=NACOS["NameSpace"],
                           username=NACOS["User"], password=NACOS["Password"])
data = client.get_config(NACOS["DataId"], NACOS["Group"])
data = json.loads(data)
print(data)


def update_cfg(args):
    print("配置产生变化")
    print(args)


CONSUL_HOST = data["consul"]["host"]
CONSUL_PORT = data["consul"]["port"]

MYSQL_DB = data["DB"]["name"]
MYSQL_HOST = data["DB"]["host"]
MYSQL_PORT = data["DB"]["port"]
MYSQL_USER = data["DB"]["user"]
MYSQL_PASSWORD = data["DB"]["password"]


SERVICE_NAME = data["service"]["name"]
SERVICE_TAGS = data["service"]["tags"]
DB = ReconnectMysqlDatabase(MYSQL_DB, host=MYSQL_HOST, port=MYSQL_PORT, user=MYSQL_USER, password=MYSQL_PASSWORD)
